import React, { Component } from 'react';
import { createPortal } from 'react-dom';
import ReactEcharts from 'echarts-for-react';
import darkEchartsTheme from '../../config/theme/darkEchartsTheme';
import echarts from 'echarts';
import { Icon } from 'antd';
import globalStyles from '../../style/common.less';

const modalRoot = document.getElementById('dialog-root');

class BasicBarChartPanel extends Component {
  constructor(props) {
    super(props);
    this.el = document.createElement('basic_info_panel');
    this.state = {
      chartOption: undefined,
      chartHeight: 280,
    };
  }
  componentDidMount() {
    modalRoot.appendChild(this.el);
    echarts.registerTheme('theme_name', darkEchartsTheme);
    this.setChartData();
  }

  setChartData() {
    const dataSource = [
        [573, '东虹桥    '],
        [4522, '北虹桥    '],
        [4561, '西虹桥    '],
        [21978, '南虹桥    '],
      ],
      min = 573,
      max = 21978;
    const option = {
      dataset: {
        source: dataSource,
      },
      tooltip: {
        trigger: 'item',
        formatter: '{b} : {c}',
      },
      legend: {
        show: false,
      },
      grid: {
        top: '5%',
        left: '3%',
        right: '15%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis: { show: false, name: '' },
      yAxis: { axisTick: { show: false }, axisLine: { show: false },splitLine:{show:false},type: 'category' },
      visualMap: {
        show: false,
        orient: 'horizontal',
        left: 'center',
        min: min,
        max: max,
        text: ['最大值', '最小值'],
        // Map the score column to color
        dimension: 0,
        inRange: {
          // color: ['#83bff6', '#188df0'],
        },
      },
      series: [
        {
          type: 'bar',
          barMaxWidth: 10,
          barGap: '10%',
          barCategoryGap: '10%',
          itemStyle: {
            normal: {
              barBorderRadius: 18,
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 1,
                  color: '#00feff',
                },
                {
                  offset: 0.5,
                  color: '#027eff',
                },
                {
                  offset: 0,
                  color: '#0286ff',
                },
              ]),
            },
          },
          label: {
            normal: {
              show: true,
              position: 'right',
              textStyle: {
                color: '#fff',
                fontSize: 12,
              },
            },
          },
          encode: {
            // Map the "amount" column to X axis.
            x: '值',
            // Map the "product" column to Y axis
            y: '地区',
          },
        },
      ],
    };
    this.setState({ chartOption: option });
  }

  render() {
    const { style, width, height } = this.props;
    const dtTitle = `虹桥商务区法人数（2018年）`;
    const { chartOption } = this.state;
    return createPortal(
      <div
        style={{
          ...style,
          display: 'flex',
          flexDirection: 'column',
          width: width,
          height: height,
          color: '#fff',
          backgroundColor: 'rgba(0,0,0,0.6)',
          boxShadow: '0 1px 2px rgba(0,0,0,0.16), 0 1px 2px rgba(0,0,0,0.23)',
        }}
      >
        <div
          style={{
            lineHeight: '35px',
            height: 35,
            borderBottom:'1px solid rgb(238, 238, 238,0.2)',
            textAlign: 'center',
            fontWeight: 'bold'
          }}
        >
          <div style={{ float: 'left', marginLeft: 6,fontSize:15}}><div style={{float: 'left',width:5,height:35,marginRight: 10,backgroundColor: 'rgba(63,177,277,1)',}}/>{dtTitle}</div>
          <div style={{ float: 'right', marginRight: 8 }}>
            {/* <Icon
              type="close"
              className={`${globalStyles.cursor_style_pointer}`}
              style={{ fontSize: 18 }}
              onClick={this.props.onClose}
            /> */}
          </div>
        </div>
        <div style={{ margin: '3px 8px 3px 8px' }}>
          <div>
            {chartOption !== undefined && (
              <ReactEcharts
                option={chartOption}
                notMerge={true}
                lazyUpdate={true}
                theme={'theme_name'}
                style={{ height: this.state.chartHeight }}
              />
            )}
          </div>
        </div>
      </div>,
      this.el
    );
  }
}

export default BasicBarChartPanel;
